﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IRUM.Utils
{
    public partial class IRUM_OptionBoard : UserControl
    {
        float _originPrice = 0;
        int _weekCount = 0;

        public IRUM_OptionBoard()
        {
            InitializeComponent();
        }

        public void SetData(DataSet ds, float originPrice, int weekCount)
        {
            _originPrice = originPrice;
            _weekCount = weekCount;

            dataGridView1.Rows.Clear();

            List<OptionBoardData> lstBoardData = new List<OptionBoardData>();
            
            DataTable dt = ds.Tables[0];
            List<string> lstShcode = dt.AsEnumerable().Select(p => p["shcode"].ToString().Substring(1)).Distinct().ToList();

            foreach (string shcode in lstShcode)
            {
                DataTable dtCall = dt.AsEnumerable().Where(p => p["shcode"].Equals("2" + shcode)).OrderBy(p => p["date"]).CopyToDataTable();
                DataTable dtPut = dt.AsEnumerable().Where(p => p["shcode"].Equals("3" + shcode)).OrderBy(p => p["date"]).CopyToDataTable();

                int currIndex = dtCall.Rows.Count == 2 ? 1 : 0;

                float prevCloseCall = Convert.ToSingle(dtCall.Rows[0]["close"]);
                float prevClosePut = Convert.ToSingle(dtPut.Rows[0]["close"]);

                OptionBoardData data = new OptionBoardData();
                data.exercisePrice = IRUM_Utils.GetExercisePriceFromShcode("2" + shcode);
                data.lowCall = Convert.ToSingle(dtCall.Rows[currIndex]["low"]);
                data.highCall = Convert.ToSingle(dtCall.Rows[currIndex]["high"]);
                data.openCall = Convert.ToSingle(dtCall.Rows[currIndex]["open"]);
                data.closeCall = Convert.ToSingle(dtCall.Rows[currIndex]["close"]);

                data.lowPut = Convert.ToSingle(dtPut.Rows[currIndex]["low"]);
                data.highPut = Convert.ToSingle(dtPut.Rows[currIndex]["high"]);
                data.openPut = Convert.ToSingle(dtPut.Rows[currIndex]["open"]);
                data.closePut = Convert.ToSingle(dtPut.Rows[currIndex]["close"]);

                if (currIndex > 0)
                {
                    data.changeCall = data.closeCall - prevCloseCall;
                    data.rateCall = data.changeCall / prevCloseCall;

                    data.changePut = data.closePut - prevClosePut;
                    data.ratePut = data.changePut / prevClosePut;
                }

                lstBoardData.Add(data);
            }

            lstBoardData.Reverse();
            foreach (OptionBoardData data in lstBoardData)
            {
                dataGridView1.Rows.Add(data.lowCall.ToString("0.00"), data.highCall.ToString("0.00"), data.openCall.ToString("0.00"), data.rateCall.ToString("P"), data.changeCall.ToString("0.00"), data.closeCall.ToString("0.00"), data.exercisePrice.ToString("0.0"), data.closePut.ToString("0.00"), data.changePut.ToString("0.00"), data.ratePut.ToString("P"), data.openPut.ToString("0.00"), data.highPut.ToString("0.00"), data.lowPut.ToString("0.00"));
            }

            ApplyStyle();
        }

        private void ApplyStyle()
        {
            float upPrice = _originPrice + _weekCount * 2.5f;
            float dnPrice = _originPrice - _weekCount * 2.5f;

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                Color colorCall = Color.Black;
                Color colorPut = Color.Black;
                float changeCall = Convert.ToSingle(row.Cells["changeCall"].Value);
                float changePut = Convert.ToSingle(row.Cells["changePut"].Value);
                float exerPrice = Convert.ToSingle(row.Cells["exercisePrice"].Value);

                if (changeCall > 0)
                {
                    colorCall = Color.OrangeRed;
                }
                else if (changeCall < 0)
                {
                    colorCall = Color.RoyalBlue;
                }

                if (changePut > 0)
                {
                    colorPut = Color.OrangeRed;
                }
                else if (changePut < 0)
                {
                    colorPut = Color.RoyalBlue;
                }

                row.Cells["lowCall"].Style.ForeColor = colorCall;
                row.Cells["highCall"].Style.ForeColor = colorCall;
                row.Cells["openCall"].Style.ForeColor = colorCall;
                row.Cells["rateCall"].Style.ForeColor = colorCall;
                row.Cells["changeCall"].Style.ForeColor = colorCall;
                row.Cells["closeCall"].Style.ForeColor = colorCall;
                row.Cells["lowPut"].Style.ForeColor = colorPut;
                row.Cells["highPut"].Style.ForeColor = colorPut;
                row.Cells["openPut"].Style.ForeColor = colorPut;
                row.Cells["ratePut"].Style.ForeColor = colorPut;
                row.Cells["changePut"].Style.ForeColor = colorPut;
                row.Cells["closePut"].Style.ForeColor = colorPut;

                if (exerPrice == _originPrice)
                {
                    row.Cells["exercisePrice"].Style.BackColor = Color.LightPink;
                }
                else
                {
                    row.Cells["exercisePrice"].Style.BackColor = Color.LightGray;
                }

                if (exerPrice == upPrice || exerPrice == dnPrice)
                {
                    row.Cells["lowCall"].Style.BackColor = Color.Yellow;
                    row.Cells["highCall"].Style.BackColor = Color.Yellow;
                    row.Cells["openCall"].Style.BackColor = Color.Yellow;
                    row.Cells["rateCall"].Style.BackColor = Color.Yellow;
                    row.Cells["changeCall"].Style.BackColor = Color.Yellow;
                    row.Cells["closeCall"].Style.BackColor = Color.Yellow;
                    row.Cells["lowPut"].Style.BackColor = Color.Yellow;
                    row.Cells["highPut"].Style.BackColor = Color.Yellow;
                    row.Cells["openPut"].Style.BackColor = Color.Yellow;
                    row.Cells["ratePut"].Style.BackColor = Color.Yellow;
                    row.Cells["changePut"].Style.BackColor = Color.Yellow;
                    row.Cells["closePut"].Style.BackColor = Color.Yellow;
                }
            }

        }
    }

    public class OptionBoardData
    {
        public float lowCall = 0;
        public float highCall = 0;
        public float openCall = 0;
        public float rateCall = 0;
        public float changeCall = 0;
        public float closeCall = 0;
        public float exercisePrice = 0;
        public float lowPut = 0;
        public float highPut = 0;
        public float openPut = 0;
        public float ratePut = 0;
        public float changePut = 0;
        public float closePut = 0;
    }
}
